package com.yc.sole.common.config;

import com.yc.sole.common.enums.CommonState;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;

/**
 * @author yizuomin
 * @date 2024/1/3 20:29
 **/
@ControllerAdvice
public class EnumSerializeConfig {

    /**
     * 生效的前提是必须使用@ControllerAdvice注解
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(CommonState.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                setValue(CommonState.getByValue(text));
            }
        });
    }
}
